package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Employee;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.BinService;
import com.itheima.vo.DishBinVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * bin service
 */
@Service
public class BinServiceImpl implements BinService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 回收站分页查询
     * @param page
     * @param pageSize
     * @param isDeleted
     * @param name
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public Page<DishBinVo> binPage(Integer page, Integer pageSize, Integer isDeleted, String name, LocalDateTime beginTime, LocalDateTime endTime) {
        if (page < 1) {
            page = 1;
        }
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getIsDeleted, isDeleted);
        queryWrapper.like(StringUtils.isNotBlank(name), Dish::getName, name);
        queryWrapper.gt(beginTime != null, Dish::getUpdateTime, beginTime);
        queryWrapper.lt(endTime != null, Dish::getUpdateTime, endTime);
        dishMapper.selectPage(dishPage, queryWrapper);

        Page<DishBinVo> dishBinVoPage = new Page<>();
        BeanUtils.copyProperties(dishPage, dishBinVoPage, "records");
        List<Dish> dishList = dishPage.getRecords();
        if (dishList != null) {
            List<DishBinVo> list = dishList.stream().map((item) -> {
                DishBinVo dishBinVo = new DishBinVo();
                BeanUtils.copyProperties(item, dishBinVo);
                Long categoryId = item.getCategoryId();
                if (categoryId != null) {
                    Category category = categoryMapper.selectById(categoryId);
                    if (category != null) {
                        dishBinVo.setCategoryName(category.getName());
                    }
                }
                Long updateUser = item.getUpdateUser();
                if (updateUser != null) {
                    Employee employee = employeeMapper.selectById(updateUser);
                    if (employee != null) {
                        dishBinVo.setUpdateUserName(employee.getUsername());
                    }
                }
                return dishBinVo;
            }).collect(Collectors.toList());

            dishBinVoPage.setRecords(list);
        }
        return dishBinVoPage;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public int binDelete(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        List<Long> list = Arrays.asList(ids);
        int i = dishMapper.deleteBatchIds(list);
        return i;
    }

    /**
     * 回收站复原
     * @param ids
     * @return
     */
    @Override
    public int binStatus(Long[] ids) {
        int res = 0;
        if (ids == null || ids.length == 0) {
            return res;
        }
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setIsDeleted(0);
            res += dishMapper.updateById(dish);
        }
        return res;
    }
}
